package com.xuan.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuan.common.util.PageResult;
import com.xuan.entity.Paper;
import com.xuan.entity.Type;
import com.xuan.mapper.PaperMapper;
import com.xuan.service.IPaperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.service.ITypeService;
import com.xuan.valueobject.ValueObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xuan
 * @since 2025-01-12
 */
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements IPaperService {

    @Autowired
    private ITypeService typeService;
    // 分页查询
    @Override
    public PageResult<Paper> findByPage(Integer page, Integer pageSize) {
        //  自己配置了分页接口
        Page<Paper> page2 = this.page(new Page<>(page, pageSize));

        //  封装分页结果
        return new PageResult<>(page2.getRecords(),page2.getTotal());
    }

    @Override
    public PageResult<Paper> find(Integer page, Integer pageSize, ValueObject vo) {
        // 搜索框搜索
/*        QueryWrapper<Paper> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getTitles()), "title", vo.getTitles());

        wrapper.eq(vo.getYears() != null , "year", vo.getYears());
        wrapper.eq(vo.getTid() != null && vo.getTid() != 0, "tid", vo.getTid());*/
        QueryWrapper<Paper> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(vo.getTitles())) {
            wrapper.like("title", vo.getTitles());
        }
        if (vo.getYears() != null) {
            wrapper.eq("year", vo.getYears());
        }
        if (vo.getTid() != null && vo.getTid() != 0) {
            wrapper.eq("tid", vo.getTid());
        }
        Page<Paper> page2 = this.page(new Page<>(page, pageSize), wrapper);

        page2.getRecords().forEach(pa -> {
            Type type = typeService.getById(pa.getTid());
            pa.setTname(type.getTname());
        });
        return new PageResult<>(page2.getRecords(),page2.getTotal());
    }
    // 新增论文
    @Override
    public boolean add(Paper paper) {
        return this.save(paper);
    }

}
